﻿using System;
using System.Windows.Forms;
using MECPSDB.DatabaseSpecific;
using MECPSDB.EntityClasses;
using MECPSDB.HelperClasses;
using Pipo.AeExtensions;
using SD.LLBLGen.Pro.ORMSupportClasses;

namespace MEC.WMS.FileMaintenance.SystemUser {
    public partial class DialogChangePassword:DevExpress.XtraEditors.XtraForm {
        private readonly string _cs;
        private readonly SystemuserEntity _systemuserEntity;
        private bool _changed;
        public DialogChangePassword(string cs,  SystemuserEntity xoperator){
            _cs = cs;
            _systemuserEntity = xoperator;
            InitializeComponent();
            _changed = false;
        }

        public bool Showme(){
            CenterToScreen();
            ShowDialog();
            return _changed;
        }

        private void BtnokClick(object sender,EventArgs e) {
            try{
                dxErrorProvider1.ClearErrors();
                bool focused = false;
                if (string.IsNullOrWhiteSpace(texteditpassword1.Text)){
                    dxErrorProvider1.SetError(texteditpassword1, "Required");
                    texteditpassword1.Focus();
                    focused = true;
                }
                if (string.IsNullOrWhiteSpace(texteditpassword2.Text)){
                    dxErrorProvider1.SetError(texteditpassword2, "Required");
                    if (!focused){
                        texteditpassword2.Focus();
                        focused = true;
                    }
                }
                if (dxErrorProvider1.HasErrors){
                    return;
                }

                if (texteditpassword1.Text != texteditpassword2.Text){
                    dxErrorProvider1.SetError(texteditpassword2, "Password does not match.");
                    texteditpassword2.Focus();
                    focused = true;
                }

                if (texteditpassword1.Text == _systemuserEntity.Password){
                    dxErrorProvider1.SetError(texteditpassword1, "Required");
                    if (focused){
                           texteditpassword1.Focus();
                            focused = true;
                    }
                 AskUser.PopupConfirmationMessage("You were requested to change you password with a new one.");
                }
       if (dxErrorProvider1.HasErrors){
                    return;
                }

                using (DataAccessAdapter dataAccessAdapter = new DataAccessAdapter(_cs)){
                    SystemuserEntity systemuserEntity = new SystemuserEntity();
                    systemuserEntity.Password = texteditpassword1.Text;
                    systemuserEntity.Rcpnl = false;
                    int updateEntitiesDirectly = dataAccessAdapter.UpdateEntitiesDirectly(systemuserEntity, new RelationPredicateBucket(SystemuserFields.UserCode== _systemuserEntity.UserCode));
                    if (updateEntitiesDirectly == 1){
                        _changed = true;
                        AskUser.PopupConfirmationMessage("Change password Sucessful!");
                        Close();
                        return;
                    }
                    AskUser.PopupConfirmationMessage("There was a problem in changing your password. \n Please retry. \n Sorry for the inconvenience.");
                    return;
                }
            } catch (Exception ex){
                MessageBox.Show(ex.Message);
            }
        }
    }
}